package com.ffcs.crmd.tsp.core.netty.server;

import com.ffcs.crmd.tsp.api.netty.INettyRemotingServer;
import com.ffcs.crmd.tsp.core.netty.NettyDecoder;
import com.ffcs.crmd.tsp.core.netty.NettyEncoder;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.LengthFieldPrepender;

/**
 * 
 * 功能说明:netty服务端初始化类，用来加载Handler、解码器等
 *
 * @author ZHONGFUHUA
 * 
 * @since 1.0.0-SNAPSHOT
 *
 */
public class NettyServerInitializer extends ChannelInitializer<SocketChannel>{

	private INettyRemotingServer remotingServer;
	
	
	public NettyServerInitializer(INettyRemotingServer remotingServer){
		this.remotingServer = remotingServer;
	}
	
    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();
        pipeline.addLast(remotingServer.getDefaultEventExecutorGroup());
        pipeline.addLast(new LengthFieldPrepender(4, false));
        pipeline.addLast(new NettyEncoder());
        pipeline.addLast(new NettyDecoder());
        //pipeline.addLast(new IdleStateHandler(0, 0, remotingServer.getServerChannelMaxIdleTimeSeconds()));
        pipeline.addLast(new NettyConnetManageHandler(remotingServer));
        pipeline.addLast(new NettyServerHandler(remotingServer));
    }
    
}
